package
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import war.Ball;
	
	public class Main extends Sprite
	{
		private var whiteBall:Ball;
		private var blackBall:Blackball;
		private var cue:Cue;
		
		/**
		 * 弧度
		 */		
		private var radians:Number;
		/**
		 * 角度
		 */		
		private var degrees:Number;
		/**
		 * 
		 */		
		private var speedX:Number = 0;
		/**
		 * 
		 */		
		private var speedY:Number = 0;
		/**
		 * 
		 */		
		private var cueSpeedX:Number = 0;
		/**
		 * 
		 */		
		private var cueSpeedY:Number = 0;
		/**
		 * 摩擦力
		 */		
		private var friction:Number = 0.98;
		/**
		 * 反弹力
		 */		
		private var Tension:Number = 0.98;
		public function Main()
		{
			init();
		
		}
		/**
		 * 
		 * 
		 */		
		private  function init():void
		{
			whiteBall=new Ball(Whiteball);
			cue=new Cue();
			stage.addEventListener(Event.ENTER_FRAME,onFrame);
			stage.addEventListener(MouseEvent.CLICK,onMouseClick);
			whiteBall.x=200;
			whiteBall.y=200;
		}
		/**
		 * 
		 * @param event
		 * 
		 */		
		protected function onFrame(event:Event):void
		{
			addChild(whiteBall);
			
			addChild(cue);
			
			var dx:Number = stage.mouseX - whiteBall.x;
			var dy:Number = stage.mouseY - whiteBall.y;
			radians = Math.atan2(dy,dx);//弧度
			
			degrees = radians * 180 / Math.PI;//角度
			
			cueSpeedX=Math.cos(degrees*Math.PI/180)*whiteBall.width/2;
			cueSpeedY=Math.sin(degrees*Math.PI/180)*whiteBall.width/2;
			
			cue.x=stage.mouseX;
			cue.y=stage.mouseY;
			
			if (whiteBall.width/2 > Math.abs( stage.mouseX-whiteBall.x))
			{
				cue.x = whiteBall.x + cueSpeedX;
			}
			if (whiteBall.width/2 >Math.abs(stage.mouseY-whiteBall.y))
			{
				cue.y = whiteBall.y + cueSpeedY;
			}
			
			speedX = speedX * friction;
			speedY = speedY * friction;
			
			cue.rotation = degrees;
			
			whiteBall.x +=  speedX;
			whiteBall.y +=  speedY;
			onHit();
			if (speedX<0.1&&speedX>-0.1)
			{
				speedX = 0;
			}
			if (speedY<0.1&&speedY>-0.1)
			{
				speedY = 0;
				
			}
			if (speedX==0||speedY==0)
			{
				cue.visible= true;
			}
			whiteBall.rotation +=  speedX * 3;
		}
		/**
		 * 球碰四面墙			
		 * 
		 */		
		private function onHit():void
		{
			if (whiteBall.x < whiteBall.width / 2 || whiteBall.x > stage.stageWidth - whiteBall.width / 2)
			{
				speedX =  -  speedX * Tension;
				whiteBall.x = (whiteBall.x > (stage.stageWidth - whiteBall.height/2)) ? (stage.stageWidth - whiteBall.height/2) : (whiteBall.height/2);
			}
			if (whiteBall.y < whiteBall.width / 2 || whiteBall.y > stage.stageHeight - whiteBall.width / 2)
			{
				speedY =  -  speedY * Tension;
				whiteBall.y = (whiteBall.y > (stage.stageHeight - whiteBall.height/2)) ? (stage.stageHeight - whiteBall.height/2) : (whiteBall.height/2);
			}
		}
		
		private function onMouseClick(e:MouseEvent):void
		{
			cue.visible= false;
			speedX -= (cue.x - whiteBall.x);
			speedY -= (cue.y - whiteBall.y);
		}
	}
}